#! /usr/bin/python
# Copyright (C) 2008  Johan Jordaan (www.johanjordaan.co.za)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

def isdigit(char):
    return char.isdigit()

def iscolor(char):
    return "WUBRG".find(char)!=-1

def isx(char):
    return "X"==char

def iscompositestart(char):
    return "("==char

def iscompositeend(char):
    return ")"==char

def iscompositesplit(char):
    return "/"==char


class Cost:
    def __init__(self,cost_string):
        self.cost_string = cost_string
        self.split()

    def __str__(self):
        return self.cost_string+" -> "+str(self.tokens)
    
    def hascost_cmc_color_isgold(self):
        has_cost = True
        cost = 0
        color = ""
        gold_count = 0

        for t in self.tokens:
            if type(t)==type([]):
                cc = 0
                for ct in t:
                    if isdigit(ct):
                        if int(ct)>cc:
                            cc = int(ct)
                    if iscolor(ct):
                        if color.find(ct)==-1:
                            color = color +ct
                        if 1>cc:
                            cc = 1
                cost = cost + cc            
            else:
                if isdigit(t):
                    cost = cost + int(t)
                if iscolor(t):
                    if color.find(t)==-1:
                        color = color +t
                        gold_count = gold_count + 1
                    cost = cost +1
        if len(self.tokens)==0:
          color = "Land"
          has_cost = False
                    
        if color=="":
            color = "Colorless"
        
        return has_cost,cost,color,gold_count>1
        
    
    def split(self):
        #print 'Splitting ',self
        state = "None"
        self.tokens = []
        temp = ""
        for c in self.cost_string:
            #print state,c,temp
            
            if isdigit(c):
                #print c,' is a digit'
                if state=="None":
                    temp = c
                    state = "Number"
                elif state=="Number":
                    temp = str(temp) + str(c)
                elif state=="Composite":
                    temp = temp + c
            elif iscolor(c):
                #print c,' is a color'
                if state=="None":
                    self.tokens.append(c)
                elif state=="Number":
                    self.tokens.append(temp)
                    self.tokens.append(c)
                    state = "None"
                elif state=="Composite":
                    temp = temp + c
            elif isx(c):
                #print c,' is a variable colorless'
                if state=="None":
                    self.tokens.append(c)
                elif state=="Number":
                    self.tokens.append(temp)
                    self.tokens.append(c)
                    state = "None"
                elif state=="Composite":
                    temp = temp + c
            elif iscompositestart(c):
                #print c,' start of a coposite'
                if state=="None":
                    temp = ""
                    state = "Composite"
                elif state=="Number":
                    self.tokens.append(temp)
                    temp = ""
                    state = "Composite"
            elif iscompositeend(c):
                if state=="Composite":
                    #print c,' end of a coposite'
                    self.tokens.append(Cost(temp).tokens)
                    state = "None"
            elif iscompositesplit(c):
                if state=="Composite":
                    #print c,' composite split'
                    temp = temp + c

        if state=="Number":
            self.tokens.append(temp)
            
        #print 'Splitting ',self,' yielded ',self.tokens

def test():
  costs = ["","0","2B","2BB","13RG","BBB","XXR","XRR","(U/B)","2(U/B)","(2/B)(2/B)"]

  for c in [Cost(cost) for cost in costs]:
    print c,(c.hascost_cmc_color_isgold())
       
if __name__ == '__main__':
    test()
        
        
